<div class="row">
    <div class="col-md-6">
        <div class="box-widget">
            <table id="list-group" class="table data-tbl-boxy responsive">
                <thead>
                    <tr>
                        <th>
                            <?php echo $this->translate('STT'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('id'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('date'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('subtotal'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('totalpaid'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('view detail'); ?>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 0; ?>
                    <?php $sumtotal = 0; $sumpaid = 0; ?>
                    <?php foreach($this->data as $row): ?>
                    <?php $i++; ?>
                    <tr>
                        <td>
                            <?php echo $i; ?>
                        </td>
                        <td>
                            <?php echo $row->id; ?>
                        </td>
                        <td>
                            <?php echo date("d-m-Y", strtotime($row->date)); ?>
                        </td>
                        <td>
                            <?php echo number_format($row->subtotal, 0, null, "."); ?>
                            <?php $sumtotal += $row->subtotal; $sumpaid += $row->totalpay; ?>
                        </td>
                        <td>
                            <?php echo number_format($row->totalpay, 0, null, "."); ?>
                        </td>
                        <td>
                            <a href="javascript:viewPrint('<?php echo $row->id; ?>')" id="viewdetail"  title="<?php echo $this->translate('view'); ?>" class="btn btn-mini btn-info">
                                <?php echo $this->translate('view'); ?>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3"><?php echo $this->translate('total') ?></td>
                        <td colspan="1"><?php echo number_format($sumtotal, 0, null, "."); ?></td>
                        <td colspan="2"><?php echo number_format($sumpaid, 0, null, "."); ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="box-widget">
            <table id="payment" class="table data-tbl-boxy responsive">
                <thead>
                    <tr>
                        <th style="width: 5%;">
                            <?php echo $this->translate('STT'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('date'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('Đã trả tiền'); ?>
                        </th>
                        <th>
                            <?php echo $this->translate('Hủy'); ?>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 0; ?>
                    <?php $totalpayment = 0; ?>
                    <?php foreach($this->payment as $row): ?>
                    <?php $i ++; ?>
                    <tr>
                        <td>
                            <?php echo $i; ?>
                        </td>
                        <td>
                            <?php echo date("h:i:s d-m-Y", strtotime($row->date)); ?>
                        </td>
                        <td>
                            <?php echo number_format($row->total, 0, null, "."); ?>
                            <?php $totalpayment += $row->total; ?>
                        </td>
                        <td>
                            <a onclick="return confirmRemove()" class="btn btn-mini btn-danger" href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-liabilities', 'action' => 'remove-payment', 'id' => $row->id, 'customer_id' => $this->customer_id), null, true); ?>">
                                <?php echo $this->translate('remove'); ?>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="2"><?php echo $this->translate('Đã thanh toán cuối') ?></td>
                        <td colspan="2"><?php echo number_format($totalpayment, 0, null, "."); ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
<script>
function remove(id){
    var ok = confirm("<?php echo $this->translate('delete order'); ?>");
    
    if(ok){
        var form = '<form action="<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'remove'), null, true); ?>" method="post" id="edit-user-' + id + '">';
            form += '<input type="hidden" name="order_id" value="' + id + '" />';
            form += '</form>';
        $("table").append(form);
        $("#edit-user-" + id).submit();
    }
}

function viewPrint(id){
    window.open('<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'view'), null, true); ?>/order_id/' + id, 'Chi tiết', 'width: 200, height: 200');
}

$(function() {
    $('.data-tbl-boxy').dataTable({
        "sPaginationType": "bootstrap",
        "iDisplayLength": 25,
        "oLanguage": {
            "sLengthMenu": "<span class='lengthLabel pull-left'><?php echo $this->translate('entries_per_page'); ?>:</span><span class='lenghtMenu pull-left'> _MENU_</span>",
            "sInfo": "<?php echo $this->translate('show start end records of total'); ?>",
            "sSearch": "<?php echo $this->translate('filter'); ?>:",
            "oPaginate": {
                "sFirst": "<?php echo $this->translate('first page'); ?>",
                "sLast": "<?php echo $this->translate('last page'); ?>",
                "sNext": "<?php echo $this->translate('next page'); ?>",
                "sPrevious": "<?php echo $this->translate('previous page'); ?>"
            }
        },
        "sDom": '<"widget-head clearfix"fl>,<"widget-container"<"widget-block"<"widget-content"t>,,<"table-bottom clearfix"<"tbl-pagination pull-left"p><"tbl-data-info pull-right"i>>'
    });
});
</script>